package com.vivo.tasks.vivofx;


import com.thinkgem.jeesite.common.persistence.Page;
import com.vivo.common.DatabaseContextHolder;
import com.vivo.retail.entity.VivoConsumerExchangeOrder;
import com.vivo.retail.service.VivoConsumerExchangeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Component
public class TaskConsumerExchange {

    @Autowired
    private VivoConsumerExchangeOrderService vivoConsumerExchangeOrderService;

    public void SyncData() {
        GetConsumeExchangeOrder("ah");
        GetConsumeExchangeOrder("js");
    }

    private void GetConsumeExchangeOrder(String area) {
        System.out.println("get consumer exchange order from "+ area + " db start...");
        Calendar cal = Calendar.getInstance();
        Date startDate = null;
        Date endDate = new Date();

        VivoConsumerExchangeOrder lastItem = new VivoConsumerExchangeOrder();
        lastItem.setTableZone(area);
        lastItem = vivoConsumerExchangeOrderService.getLast(lastItem);

        if (lastItem == null) {
            cal.set(2016, Calendar.JULY, 1);
            startDate = cal.getTime();
        } else {
            startDate = lastItem.getMakeDate();
        }

        System.out.println("consumer exchange last make time is: " + startDate);

        int pageStart = 1;
        int pageSize = 5000;

        VivoConsumerExchangeOrder queryOrder = new VivoConsumerExchangeOrder();
        queryOrder.setBeginMakeDate(startDate);
        queryOrder.setEndMakeDate(endDate);

        Page<VivoConsumerExchangeOrder> pageParam = new Page<VivoConsumerExchangeOrder>(pageStart, pageSize);
        pageParam.setOrderBy("b.makedate");

        try {
            Helper.changeDB(area);

            Page<VivoConsumerExchangeOrder> pageRes = vivoConsumerExchangeOrderService.findPage(pageParam, queryOrder);
            DatabaseContextHolder.setMainDb();

            SaveConsumerOrderList(pageRes.getList(), area);

            for (int i = pageStart + 1; i <= pageRes.getTotalPage(); i++) {
                System.out.println("total page is: " + pageRes.getTotalPage() + " cur page is : " + pageRes.getPageNo() + " and pageSize is :" + pageRes.getPageSize());
                pageParam.setPageNo(i);

                Helper.changeDB(area);

                pageRes = vivoConsumerExchangeOrderService.findPage(pageParam, queryOrder);
                DatabaseContextHolder.setMainDb();

                SaveConsumerOrderList(pageRes.getList(), area);
            }
        } finally {
            DatabaseContextHolder.setMainDb();
        }
    }

//    private Page<VivoConsumerExchangeOrder> getPage(Page<VivoConsumerExchangeOrder> pageParam, VivoConsumerExchangeOrder queryOrder) {
//        try {
//
//        }
//    }


    private void SaveConsumerOrderList(List<VivoConsumerExchangeOrder> list, String area) {
        List<VivoConsumerExchangeOrder> orderList = new ArrayList<VivoConsumerExchangeOrder>();

        for (VivoConsumerExchangeOrder order : list) {
            order.setIsTaskInsert(true);
            order.setOrderCode(order.getOrderCode().substring(0, 14));
            order.setTableZone(area);
            orderList.add(order);

            if (orderList.size() >= 1000) {
                saveBatchOrders(orderList, area);

                orderList.clear();
            }
        }

        if (orderList.size() > 0) {
            saveBatchOrders(orderList, area);
        }
    }

    private void saveBatchOrders(List<VivoConsumerExchangeOrder> orderList, String area) {
        try {
            vivoConsumerExchangeOrderService.insertByBatch(orderList, area);
        } catch (Exception e) {
            e.printStackTrace();

            for (VivoConsumerExchangeOrder order: orderList) {
                try {
                    vivoConsumerExchangeOrderService.save(order);
                } catch (Exception ex) {
                    System.out.println("顾客退货数据已经存在！ err: " + ex.getMessage());
                }
            }
        }
    }
}
